package com.saa.application.service;

//import com.github.pagehelper.PageHelper;
//import com.github.pagehelper.PageInfo;


import com.saa.application.dao.SaaApplicationInfoDao;
import com.saa.application.po.SaaApplicationInfo;
import com.saa.application.pojo.PageBean;
import com.saa.application.vo.SaaApplicationInfoVo;
import com.saa.common.vo.OmsResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApplicationInfoService {
    public static final Logger logger = LoggerFactory.getLogger(ApplicationInfoService.class);

    @Autowired
    private SaaApplicationInfoDao saaApplicationInfoDao;

    //增加应用
    public OmsResultVo insertApplicationInfo(SaaApplicationInfo saaApplicationInfo) {

        if(saaApplicationInfo.getApplicationId()!=null){
            saaApplicationInfoDao.updateSelectiveByPrimaryKey(saaApplicationInfo);
            return OmsResultVo.builder(200, "修改成功", null);
        }else{
            SaaApplicationInfo app1=new SaaApplicationInfo();
            app1.setApplicationCode(saaApplicationInfo.getApplicationCode());
            SaaApplicationInfo app2=new SaaApplicationInfo();
            app2.setApplicationName(saaApplicationInfo.getApplicationName());
            List<SaaApplicationInfo> list1 = saaApplicationInfoDao.selectList(app1);
            List<SaaApplicationInfo> list2 = saaApplicationInfoDao.selectList(app2);
//        System.out.println(applicationInfoMapper.selectList(app1));
//        System.out.println(applicationInfoMapper.selectList(app2));
            if((list1 == null || list1.isEmpty()) && (list2 == null || list2.isEmpty())){

                saaApplicationInfo.setCreateTime(new Date());
                saaApplicationInfo.setCreateBy("wlwl");
                saaApplicationInfo.setUpdateTime(new Date());
                saaApplicationInfo.setUpdateBy("wlwl");
                saaApplicationInfo.setStatus("1");
                saaApplicationInfoDao.insertSelective(saaApplicationInfo);
//            System.out.println(applicationInfoMapper.insertSelective(applicationInfo));
                return OmsResultVo.builder(200, "新增成功", null);
            }else{
                return OmsResultVo.builder(500, "新增失败", null);
            }
        }


    }

    //查询应用分页
    public OmsResultVo getApplicationInfoPage(SaaApplicationInfoVo saaApplicationInfoVo) {
//        System.out.println(applicationInfo.getApplicationName());
        int page = saaApplicationInfoVo.getPage();
        int pageSize = saaApplicationInfoVo.getPageSize();
        int startIndex = (page - 1) * pageSize;
//        Map<String, Object> params = new HashMap<>();
//        params.put("startIndex",startIndex);
//        params.put("pageSize",pageSize);
//        params.put("applicationInfo",applicationInfo);
        saaApplicationInfoVo.setStartIndex(startIndex);
        List<SaaApplicationInfo> applicationInfoList = saaApplicationInfoDao.selectPage(saaApplicationInfoVo);
        Long total = saaApplicationInfoDao.selectTotal(saaApplicationInfoVo);
        if(applicationInfoList!=null){
            PageBean pageBean = new PageBean();
            pageBean.setTotal(total);
            pageBean.setRows(applicationInfoList);
            return OmsResultVo.builder(200, "查询成功", pageBean);
        }else {
            return OmsResultVo.builder(500, "查询失败", null);
        }
    }

    //查询应用
    public OmsResultVo getApplicationInfo(SaaApplicationInfo saaApplicationInfo) {
        List<SaaApplicationInfo> applicationInfoList = saaApplicationInfoDao.selectList(saaApplicationInfo);
        if(applicationInfoList!=null){
            return OmsResultVo.builder(200, "查询成功", applicationInfoList);
        }else {
            return OmsResultVo.builder(500, "查询失败", null);
        }
    }

    //查询所有应用名称
    public OmsResultVo getApplicationList() {
        List<SaaApplicationInfo> applicationInfoList = saaApplicationInfoDao.getApplicationList();
        if(applicationInfoList!=null){
            return OmsResultVo.builder(200, "查询成功", applicationInfoList);
        }else {
            return OmsResultVo.builder(500, "查询失败", null);
        }
    }


    //    //删除应用
    public OmsResultVo deleteApplicationInfo(SaaApplicationInfo saaApplicationInfo) {
        Long id = saaApplicationInfo.getApplicationId();
        int res = saaApplicationInfoDao.deleteByPrimaryKey(id);
        if(res!=0){
            return OmsResultVo.builder(200, "删除成功", null);
        }else {
            return OmsResultVo.builder(500, "删除失败", null);
        }
    }
    //修改应用
    public OmsResultVo updateApplicationInfo(SaaApplicationInfo saaApplicationInfo) {
        int res = saaApplicationInfoDao.updateSelectiveByPrimaryKey(saaApplicationInfo);
        if(res!=0){
            return OmsResultVo.builder(200, "更新成功", null);
        }else {
            return OmsResultVo.builder(500, "更新失败", null);
        }
    }

    /****根据应用ID查询应用名称***/
    public String getSaaApplicationInfo(Long applicationCode) {
        SaaApplicationInfo saaApplicationInfo = saaApplicationInfoDao.selectByPrimaryKey(applicationCode);
        if(saaApplicationInfo!=null){
            return saaApplicationInfo.getApplicationName();
        }
        return null;
    }
}
